package com.example.automatic.abs_video;

import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.GestureDescription;
import android.graphics.Path;
import android.graphics.Rect;
import android.os.Build;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import androidx.annotation.RequiresApi;

import com.example.automatic.utils.LogUtils;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

public class DYAccessibilityService extends AccessibilityService {

    @Override
    protected void onServiceConnected() {
        super.onServiceConnected();
        Log.e("TAG", "onServiceConnected=========================");
    }

    @Override
    public void onAccessibilityEvent(final AccessibilityEvent event) {
        AccessibilityNodeInfo accessibilityNodeInfo1 = getRootInActiveWindow();
        if (accessibilityNodeInfo1 != null) {
//            if (accessibilityNodeInfo1.toString().contains("加入了直播间")) {
//                LogUtils.e(event.getClassName() + "，" + accessibilityNodeInfo1.getClassName() + "，，，，" + event.toString());
//            }
//            LogUtils.e(event.getClassName() + "，" + accessibilityNodeInfo1.getClassName() + "，，，，" + event.toString());
            LogUtils.e(accessibilityNodeInfo1.getText() + "\n" + event.toString());

        }


//        if (event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
//            if (event.getClassName().equals("com.ss.android.ugc.aweme.main.MainActivity")) {
//
//            } else if (event.getClassName().equals("com.ss.android.ugc.aweme.update.l")) {
//                //"以后再说"按钮控件
//                AccessibilityNodeInfo alsNodeInfo =
//                        getAccessibilityNodeInfo("com.ss.android.ugc.aweme.lite:id/als", "以后再说");
//                if (alsNodeInfo == null) {
//                    return;
//                }
//                alsNodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
//            }
//        }

    }

    @Override
    public void onInterrupt() {
        Log.e("TAG", "onInterrupt=========================");
    }


    /**
     * 主页面操作
     */
    private void mainActivity() {
        //"来赚钱"小福袋按钮
        AccessibilityNodeInfo ay2NodeInfo =
                getAccessibilityNodeInfo("com.ss.android.ugc.aweme.lite:id/ay2");
        if (ay2NodeInfo == null) {
            return;
        }
        ay2NodeInfo.performAction(AccessibilityNodeInfo.ACTION_CLICK);
    }


    /**
     * 返回指定操作控件
     */
    public AccessibilityNodeInfo getAccessibilityNodeInfo(String viewId, String viewText) {
        AccessibilityNodeInfo accessibilityNodeInfo = getRootInActiveWindow();
        if (accessibilityNodeInfo == null) {
            return null;
        }
        List<AccessibilityNodeInfo> gridFunctionList = accessibilityNodeInfo
                .findAccessibilityNodeInfosByViewId(viewId);
        for (AccessibilityNodeInfo nodeInfo : gridFunctionList) {
            if (viewText == null) {
                return nodeInfo;
            } else if (nodeInfo.getText().equals(viewText)) {
                return nodeInfo;
            }
        }
        return null;
    }

    public AccessibilityNodeInfo getAccessibilityNodeInfoByText(String viewText) {
        AccessibilityNodeInfo accessibilityNodeInfo = getRootInActiveWindow();
        if (accessibilityNodeInfo == null) {
            return null;
        }
        List<AccessibilityNodeInfo> gridFunctionList = accessibilityNodeInfo
                .findAccessibilityNodeInfosByText(viewText);
        for (AccessibilityNodeInfo nodeInfo : gridFunctionList) {
            if (nodeInfo.getText().equals(viewText)) {
                return nodeInfo;
            }
        }
        return null;
    }

    public AccessibilityNodeInfo getAccessibilityNodeInfo(String viewId) {
        return this.getAccessibilityNodeInfo(viewId, null);
    }


    private AccessibilityNodeInfo findByID(AccessibilityNodeInfo rootInfo, String text) {
        if (rootInfo.getChildCount() > 0) {
            for (int i = 0; i < rootInfo.getChildCount(); i++) {
                AccessibilityNodeInfo child = rootInfo.getChild(i);
                try {
                    List<AccessibilityNodeInfo> nodeInfoList = child.findAccessibilityNodeInfosByViewId(text);
                    if (nodeInfoList.size() > 0) {
                        return nodeInfoList.get(0);
                    }
                } catch (Exception e) {
                    return null;
                }
                findByID(child, text);//递归一直找一层层的全部遍历
            }
        }
        return null;
    }
}
